package appapi.controller.oider;

import appapi.common.ResponseBean;
import appapi.entity.mqtt.Devices;
import appapi.entity.mqtt.Interference;
import appapi.service.InterferenceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @version: java version 1.8
 * @Author: Ygp Work
 * @description:
 * @date: 2023-07-05 17:29
 */
@Api(tags = "干涉塔机接口")
@RestController
@RequestMapping("/api/interference")
@CrossOrigin
public class InterferenceController {
    @Autowired
    private InterferenceService interferenceService;

    @PostMapping("/insertDevices")
    @ApiOperation(value = "更新干涉塔机")
    @CrossOrigin
    public ResponseBean insertDevices(@RequestBody Interference interference) {
        interferenceService.insertInterference(interference);
        return ResponseBean.success();
    }

    @ApiOperation(value = "根据id拿干涉塔机")
    @GetMapping("/devicesId")
    @CrossOrigin
    public ResponseBean getDevicesById(Long devicesId) {
        List<Interference> interference = interferenceService.getInterferenceById(devicesId);
        if (interference != null) {
            return ResponseBean.success(interference);
        } else {
            return ResponseBean.fail("查询失败，找不到指定的设备");
        }
    }

    @GetMapping
    @ApiOperation(value = "查询所有的干涉塔机")
    @CrossOrigin
    public ResponseBean<List<Devices>> getAllDevices(Interference interference) {
        List<Interference> devicesList = interferenceService.getAllInterference(interference);
        int size = devicesList.size();
        if (!devicesList.isEmpty()) {
            return ResponseBean.success(size, devicesList);
        } else {
            return ResponseBean.fail("查询失败，设备列表为空");
        }
    }
}
